//
//  XDMybankCardViewController.m
//  xindailianmeng
//
//  Created by 张东 on 2017/2/22.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDMybankCardViewController.h"
#import "XDBankCardFooterView.h"
#import "XDBankCordChangeController.h"
#import "AFHTTPSessionManager+POST.h"
#import "XDVerifiedViewController.h"
#import "XDSetSafePassViewController.h"
#import "XDBindBankCardViewController.h"
#import "ZHRTRequestFailureShowViewOrDetail.h"
@interface XDMybankCardViewController ()<UITableViewDelegate,UITableViewDataSource,XDBankCardFooterViewDelegate>

@property (nonatomic, weak) UITableView *tableView;
//身份证
@property (nonatomic, copy) NSString *idNumber;
//真实姓名
@property (nonatomic, copy) NSString  *realName;


@property (nonatomic,strong) XDBankCardFooterView *footerView;

@property (nonatomic, strong) NSDictionary *bankInfo;
@end

@implementation XDMybankCardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColorRGB(234, 234, 234);
    self.title = @"我的银行卡";
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self loadData];
}
- (void)loadData {
    
    NSDictionary * param = @{@"method":@"myBank"};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            
            NSDictionary *dic = responseObject[@"result"];
            
            /*
             
             code = 100000;
             message = "\U64cd\U4f5c\U6210\U529f";
             result =     {
             bankInfo =         {
             accountCity = "<null>";
             accountProvince = "<null>";
             bankCardType = "\U501f\U8bb0\U5361";
             bankCode = abc;
             bankIconUrl = "http://172.16.20.248:6060/zhrtChinaWeb/biz-assets/bankIcon/small/abc.png";
             bankId = 53;
             bankLimit = "\U6bcf\U65e53w,\U6bcf\U6b215K";
             bankName = "\U519c\U4e1a\U94f6\U884c";
             bankNumber = "6228 **** **** 4575";
             bankPhone = 15244608254;
             bankType = 1;
             brachName = "<null>";
             };
             idNumber = "232************860";
             isBindBank = Y;
             realName = "*\U5fae";
             };
             type = success;
             }
             */
   
            if ([dic[@"isBindBank"] isEqualToString:@"Y"]) {
                [self setupUI];
                self.idNumber = dic[@"idNumber"];
                
                self.realName = dic[@"realName"];
                NSDictionary *bankInfo = dic[@"bankInfo"];
                
                self.bankInfo = bankInfo;
                [self.footerView footerViewWithbankIconUrl:bankInfo[@"bankIconUrl"] bankName:bankInfo[@"bankName"] bankCardType:bankInfo[@"bankCardType"] bankNumber:bankInfo[@"bankNumber"]];
               
            } else {
                
                [self setupNoBankCardUI];
            }
        } else if ([responseObject[@"code"] isEqualToString:@"100001"]){ // 超时
            
            
        }else {
            
            
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];
    
}

//无卡时
- (void)setupNoBankCardUI {
    
    self.view.backgroundColor = kColorRGB(234, 234, 234);
    UIImage *img = [UIImage imageNamed:@"noBankCard"];
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
    imgView.frame = CGRectMake((kScreenW - img.size.width) * 0.5, kScreenH - 300 *kscale - img.size.height - 64, img.size.width, img.size.height);
    [self.view addSubview:imgView];
    
    
    UILabel *tipLb = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(imgView.frame) + 30, kScreenW, 20)];
    tipLb.text = @"您还未绑定银行卡";
    tipLb.textColor = kColorRGB(153, 153, 153);
    tipLb.font = kBaseFont_(14);
    tipLb.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:tipLb];
    
    UIImage *btnImg = [UIImage imageNamed:@"make_money"];
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake((kScreenW - btnImg.size.width) * 0.5 , CGRectGetMaxY(tipLb.frame) + 30, btnImg.size.width, btnImg.size.height)];
    [btn setBackgroundImage:btnImg forState:UIControlStateNormal];
    [btn setBackgroundImage:btnImg forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(toMakeMoney) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}

//有卡时
- (void)setupUI {
    
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH - 64) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.showsVerticalScrollIndicator = NO;
    tableView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:tableView];
    tableView.separatorInset = UIEdgeInsetsMake(0, 15, 0, 15);
    tableView.backgroundColor = [UIColor whiteColor];
    
    self.footerView = [[NSBundle mainBundle] loadNibNamed:@"XDBankCardFooterView" owner:self options:nil].firstObject;
    self.footerView.backgroundColor = [UIColor whiteColor];
    self.footerView.delegate = self;
    tableView.tableFooterView = self.footerView;
    self.tableView = tableView;
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 2;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *myID = @"XDMybankCardViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:myID];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:myID];
    }
    cell.textLabel.textColor = kColorRGB(102,102,102);
    cell.textLabel.font = kBaseFont_(14);
    if (indexPath.row == 0) {
        cell.textLabel.text = @"真实姓名";
        cell.detailTextLabel.text = self.realName;
    } else {
        cell.textLabel.text = @"身份证号";
        cell.detailTextLabel.text = self.idNumber;
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {

    return 35;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 35;
}

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 0, kScreenW, 35)];
        view.backgroundColor = kColorRGB(234, 234, 234);
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenW, 35)];
        lb.text = @"实名认证信息";
        lb.textColor = kColorRGB(102,102,102);
        lb.font = kBaseFont_(13);
        [view addSubview:lb];
        return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(15, 0, kScreenW, 35)];
    view.backgroundColor = kColorRGB(234, 234, 234);
    UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, kScreenW, 35)];
    lb.text = @"银行卡信息";
    lb.textColor = kColorRGB(102,102,102);
    lb.font = kBaseFont_(13);
    [view addSubview:lb];
    return view;
    
}

- (void)changeBankCard:(XDBankCardFooterView *)footerView {
    
    XDBankCordChangeController *change = [[XDBankCordChangeController alloc] init];
    change.bankInfo = self.bankInfo;
    [self.navigationController pushViewController:change animated:YES];
    
}

- (void)toMakeMoney {
    
    
    NSDictionary * param = @{@"method":@"getAccountStatus"};
    [AFHTTPSessionManager POSTWithParams:param success:^(NSURLSessionDataTask *task, id responseObject) {
        
        if ([responseObject[@"code"] isEqualToString:@"100000"]) {
            NSDictionary *dic = responseObject[@"result"];
            if ([dic[@"verifiedStatus"] isEqualToString:@"N"]) {
                
                XDVerifiedViewController *verified = [[XDVerifiedViewController alloc] init];
                
                [self.navigationController pushViewController:verified animated:YES];
            }else if ([dic[@"safePassStatus"] isEqualToString:@"N"]){
                
                XDSetSafePassViewController *setSafePass = [[XDSetSafePassViewController alloc] init];
                
                [self.navigationController pushViewController:setSafePass animated:YES];
            }else if ([dic[@"bindStatus"] isEqualToString:@"N"]){
                
                XDBindBankCardViewController *bindBankCard = [[XDBindBankCardViewController alloc] init];
                
                [self.navigationController pushViewController:bindBankCard animated:YES];
            }
            }  else {
                [ZHRTRequestFailureShowViewOrDetail showViewDetail:responseObject[@"message"] view:self.view];
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
         [ZHRTRequestFailureShowViewOrDetail showViewDetail:@"无网络" view:self.view];
    }];
    
}
@end
